package timetable.interfaces;

import java.io.IOException;
import java.sql.SQLException;

import timetable.exceptions.DriverNotFoundException;
import timetable.exceptions.TimeTableParserErrors;
import timetable.exceptions.ToleranceReachedException;

public class TextualInterface extends ImporterInterface {

	private static final int OPT_HELP = 1;
	private static final int OK = 0;
	private static final int FAILED = -1;
	
	
	private String[] args;
	
	public TextualInterface(String[] args)
	{
		super();
		this.args = args;
	}
	
	private int parseArguments()
	{
		int success = OK;
		for (String a : args)
		{
			String[] opt = a.split("=");
			//System.out.println(opt[0]);
			//System.out.println(opt[1]);
			if (opt[0].equals("-file"))
			{
				super.filepath = opt[1].trim();
			}
			else if (opt[0].equals("-delimiter"))
			{
				super.delimiter = opt[1].trim();
			}
			else if (opt[0].equals("-subdelimiter"))
			{
				super.subdelimiter = opt[1].trim();
			}
			else if (opt[0].equals("-help"))
				return OPT_HELP;
			else if (opt[0].equals("-clear"))
			{
				super.clearDB = (!opt[1].equals("no"));
			}
			else
			{
				success = FAILED;
				System.err.println("invalid option: '" + opt[0] + "'\n\t for help of the usage of ttimport type 'ttimport -help'");
			}
		}
		return success;
	}

	private boolean checkArguments()
	{
		if (filepath == null) 
		{
			System.err.println("you must set a file-option in order to import");
			return false;
		}
		return true;
	}
	
	private void printHelpInformation() 
	{
		System.out.println(
				"ttimport HELP information\n" +
				"=========================\n" +
				"Usage of ttimport: \n" +
				"\t" + "ttimport -file=*filepath* [options]\n" +
				"The '-file' option is always required in order to import and specifies the raw timetable information file.\n" +
				"Available options are:\n" +
				"\t" + "-delimiter=*sequence*" + "\t\t" + "Sets the delimiter of the raw timetable information file. Change *sequence* to the character sequence you want to use for delimiting.\n" +
				"\t\t\t\t\t" + "When not set the default delimiter '\\t' is used.\n" + 	
				"\t" + "-subdelimiter=*sequence*" + "\t" + "Sets the subdelimiter of the raw timetable information file. Change *sequence* to the character sequence you want to use for subdelimiting.\n" +
				"\t\t\t\t\t" + "When not set the default subdelimiter ';' is used.\n"
		
		);
	}

	@Override
	protected void handleDriverNotFoundException(DriverNotFoundException e) {
		e.printStackTrace();
		
	}

	@Override
	protected void handleIOException(IOException e) {
		e.printStackTrace();
		
	}

	@Override
	protected void handleSQLException(SQLException e) {
		e.printStackTrace();
		
	}

	@Override
	protected void handleTableParserErrors(TimeTableParserErrors e) {
		e.printStackTrace();
		
	}

	@Override
	protected void handleToleranceReachedException(ToleranceReachedException e) {
		e.printStackTrace();
		
	}

	@Override
	protected void printProgressInfo(String message) {
		System.out.println(message);
		
	}

	@Override
	protected void progressUpdate(int percent) {
		if (percent%10 == 1) System.out.print("=");
		if (percent == 100) System.out.println(" \tdone!");
	}

	@Override
	protected void postRun() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected boolean preRun() {
		
		int ret = parseArguments();
		
		if (ret == OPT_HELP)
		{
			printHelpInformation();
			return false;
		}
		else if (ret == FAILED)
			return false;
		
		
		if (!checkArguments()) return false;
		
		
		return true;
	}
}
